{include file="public/header"/}
{include file="public/add_header"/}
<form method="post" class="form-horizontal" action="save" id="layer-form">
    {if condition="$data['id']!=''"}<input type="hidden" value="{$data['id']}" name="id">{/if}
    <input type="hidden" value="{$data['menus']}" name="menus" id="menus" datatype="*" nullmsg="菜单权限不能为空！">
    <!--表单项-->
    <div class="form-group">
        <label class="col-sm-2 control-label">角色名</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="name" value="{$data['name']}" >
        </div>
    </div>
    <div class="hr-line-dashed"></div>
    <div class="form-group">
        <label class="col-sm-2 control-label">描述</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="about" value="{$data['about']}" >
        </div>
    </div>
    <div class="hr-line-dashed"></div>
    <div class="form-group">
        <label class="col-sm-2 control-label">菜单权限</label>
        <div class="col-sm-10">
            <ul id="areasTree" class="ztree"></ul>
        </div>
    </div>
    <div class="hr-line-dashed"></div>
    <!--/表单项-->
    {$html->submit_btn()}
</form>
{include file="public/add_footer"/}
{include file="public/footer"/}
<script type="text/javascript">
    var tree;
    $(function () {
        var menus=$("#menus").val();
        $.ajax({
            url: "/admin/sys.menu/getMenus?type=1&checks="+menus,
            type: 'get',
            dataType: 'json',
            success: function (res) {
                var setting = {
                    check: {
                        enable: true
                    },
                    callback:{
                        onCheck: onCheck
                    },
                    data: {
                        simpleData: {
                            enable: true
                        }
                    }
                };
                tree=$.fn.zTree.init($("#areasTree"), setting, res);
                tree.expandAll(true);
            }
        });
    });

    function  onCheck() {
        var nodes=tree.getCheckedNodes(true);
        var v="";
        for(var i=0;i<nodes.length;i++){
            v+=nodes[i].id + ",";
        }
        //去除最后的逗号
        if(v!=""){
            v=v.substr(0,v.length-1);
        }
        $("#menus").val(v);
    }
</script>